package com.zfl.config;

import com.zfl.interceptor.AuthInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvc配置：注册拦截器并配置拦截/放行路径
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    // 注入自定义拦截器
    private final AuthInterceptor authInterceptor;

    public WebMvcConfig(AuthInterceptor authInterceptor) {
        this.authInterceptor = authInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns(   // 放行以下路径（无需登录/权限）
                        // 登录/注册接口
                        "/",                // 根路径
                        "/login.html",      // 放行登录页面
                        "/index.html",      // 放行首页
                        "/success.html",
                        "/auth/login",
                        "/auth/register",
                        // 静态资源（前端页面、CSS、JS等）
                        "/static/**",
                        "/css/**",
                        "/js/**",
                        "/images/**",
                        // 接口文档（如Swagger）
                        "/swagger-ui/**",
                        "/v3/api-docs/**",
                        // 错误页
                        "/error"
                );
    }
}